package com.itheima.service.impl;

import com.itheima.mapper.UserStudentAssignmentMapper;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import com.itheima.service.UserStudentAssignmentService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserStudentAssignmentServiceImpl implements UserStudentAssignmentService {
    @Autowired
    private UserStudentAssignmentMapper userStudentAssignmentMapper;
    @Autowired
    private UserService userService;


    @Override
    public ResponseResult insert(Assignment assignment) {
        int flag= userStudentAssignmentMapper.insert(assignment);
        if (flag>0){
           return new ResponseResult(0,"操作成功",null);
        }
        return new ResponseResult(1,"操作失败",null);
    }

    @Override
    public ResponseResult findWork(HttpServletRequest request,WorkDto workDto) {
        //获取班级学生信息
        Integer classId = (Integer) request.getSession().getAttribute("ClassId");
        List<User> users = userService.findByClassId(classId);
        List<WorkUser> workUsers = new ArrayList<>();
        for (User user : users) {
            WorkUser workUser = new WorkUser();
            List<UserStudentAssignment> userStudentAssignments =userStudentAssignmentMapper.findByTimeAndTypeAndUid(workDto.getType(),workDto.getTime(), user.getId());
            List<WorkType> workTypes = new ArrayList<>();
            for (UserStudentAssignment userStudentAssignment : userStudentAssignments) {
                String assignmentName = userStudentAssignment.getAssignmentName();
                Integer id = userStudentAssignment.getId();
                Integer status = userStudentAssignment.getStatus();
                WorkType workType = new WorkType(assignmentName,id,status);
                workTypes.add(workType);

            }
            workUser.setUid(user.getId());
            workUser.setName(user.getName());
            workUser.setPracticeList(workTypes);
            workUsers.add(workUser);
        }
        return new ResponseResult(0,"操作成功",workUsers);

    }

    @Override
    public ResponseResult findNotWork(StatisticsDto statisticsDto) {
        List<Statistics> statisticsList = userStudentAssignmentMapper.findNotWork(statisticsDto);

        return new ResponseResult(0,"操作成功",statisticsList);
    }


}